package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.entity.DeviceInfo;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;

import java.util.List;


public class SelectListDialog extends Dialog implements View.OnClickListener, DevicesListViewDialog.OnclickItemCallback {
    private static final String TAG = "SelectListDialog";
    private Button btnCancel, btnConfirm;
    private EditText editText1;
    private TextView tvTips1, tvTips2;
    private EditText editText2;
    private Context context;
    private AppCompatActivity activity;
    private DoubleDialogCallback callback;
    private DevicesListViewDialog listViewDialog;
    private DeviceInfo deviceInfo;
    private List<DeviceInfo> tempList;

    public SelectListDialog(@NonNull Context context, DoubleDialogCallback callback) {
        super(context);
        this.callback = callback;
        listViewDialog = new DevicesListViewDialog(context, this);
        initView(context);
    }

    public void setEtInputType(int type1, int type2) {
        editText1.setInputType(type1);
        editText2.setInputType(type2);
    }

    public SelectListDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.double_select_list_dialog, null);
        btnCancel = root.findViewById(R.id.btn_cancel);
        btnConfirm = root.findViewById(R.id.btn_confirm);
        tvTips1 = root.findViewById(R.id.tv_tips1);
        root.findViewById(R.id.iv).setOnClickListener(this);
        tvTips2 = root.findViewById(R.id.tv_tips2);
        editText1 = root.findViewById(R.id.et_value_1);
        editText2 = root.findViewById(R.id.et_value_2);
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
        setListence();
    }

    private void setListence() {
        editText1.setFocusable(false);
        editText1.setEnabled(false);
    }


    public void showDialog(List<DeviceInfo> list) {
        tempList = list;
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                confirmData();

                break;
            case R.id.btn_cancel:
                hideKebord();
                clearData();
                dismiss();
                break;
            case R.id.iv:
                if (tempList != null && !tempList.isEmpty()) {
                    listViewDialog.showDialog(tempList);
                } else {
                    showToast("没有炒菜机数据");
                }
                break;
            default:
        }
    }

    private void confirmData(){
        if(deviceInfo==null){
            showToast("请选择炒菜机,再点击确认");
            return;
        }
        hideKebord();
        callback.doubleDialogInputResult(getValue(editText2), deviceInfo);
        dismiss();
    }

    private void hideKebord(){
        InputMethodManagerUtil.inputMethodStatus(activity, editText2, false);
    }

    public void setTips(String tips1, String tips2) {
        tvTips1.setText(tips1);
        tvTips2.setText(tips2);
    }

    private void clearData() {
        editText1.setText("");
        editText2.setText("");
        editText1.setHint("");
        editText2.setHint("");
    }

    public void setHintMsg(String ms1, String ms2) {
        editText1.setText(ms1);
        editText2.setText(ms2);
    }

    private float getValue(EditText et) {
        float temp = 0;
        try {
            String ss = et.getText().toString().trim();
            if (!TextUtils.isEmpty(ss)) {
                temp = Float.parseFloat(ss);
            }
        } catch (Exception e) {
            showToast("数据转换异常，请输入有效内容");
        }
        return temp;
    }

    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onclickItem(DeviceInfo deviceInfo) {
        Log.e(TAG, "炒菜机选择结果:" + JsonHelper.toJson(deviceInfo));
        editText1.setText(deviceInfo.getName());
        this.deviceInfo = deviceInfo;
    }

    void hideSoftBoard() {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
    }

    public interface DoubleDialogCallback {
        void doubleDialogInputResult(float weight, DeviceInfo deviceInfo);
    }
}
